/**
 * @file Assignment_F.cpp
 * @brief 
 * @author htli (3180102114@zju.edu.cn)
 * @version 1.0
 * @date 2021-10-03
 * 
 * @copyright Copyright (c) 2021  linhuo2020
 * 
 */
#include "NewtonSolver.h"
#include "SecantSolver.h"
const double l = 89;
const double h = 49;
const double beta = 11.5;
const double A = l * sin(beta * M_PI / 180.0);
const double B = l * cos(beta * M_PI / 180.0);
const double _tolerance = 1e-6;
double D = 55;
double C = (h + 0.5 * D) * sin(beta * M_PI / 180.0) - 0.5 * D * tan(beta * M_PI / 180.0);
double E = (h + 0.5 * D) * cos(beta * M_PI / 180.0) - 0.5 * D;
double f(double x)
{
    double a = sin(x);
    double b = cos(x);
    return A * a * b + B * a * a - C * b - E * a;
};

double df(double x)
{
    double a = sin(x);
    double b = cos(x);
    return A * (b * b - a * a) + B * 2.0 * a * b + C * a - E * b;
};

void translater(double x)
{
    std::cout << "The angle is " << x * 180.0 / M_PI << " degree." << std::endl;
};

int main()
{
    double guess = 33.0 * M_PI / 180.0;
    std::cout << "F.(a)" << std::endl;
    NewtonSolver M_B(f, df);
    M_B.set_init(guess);
    M_B.set_tolerance(_tolerance);
    translater(M_B.solve());

    std::cout << "F.(b)" << std::endl;
    D = 30;
    C = (h + 0.5 * D) * sin(beta * M_PI / 180.0) - 0.5 * D * tan(beta * M_PI / 180.0);
    E = (h + 0.5 * D) * cos(beta * M_PI / 180.0) - 0.5 * D;
    translater(M_B.solve());

    std::cout << "F.(c)" << std::endl;
    double guess_close = 120 * M_PI / 180.0;
    double guess_far = 348.4 * M_PI / 180.0;
    SecantSolver M_C(f);
    M_C.set_tolerance(_tolerance);
    std::cout << "First, a guess close to 33 degree, for example take another as " << guess_close * 180 / M_PI << " degree:" << std::endl;
    M_C.set_init(guess, guess_close);
    translater(M_C.solve());
    std::cout << "Next, a guess far from 33 degree, for example take another as " << guess_far * 180 / M_PI << " degree:" << std::endl;
    M_C.set_init(guess, guess_far);
    translater(M_C.solve());
};